/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.LoaiNguoiDungDAO;
import dao.NguoiDungDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dto.LoaiNguoiDung;
import dto.NguoiDung;
import java.sql.SQLException;
 
public class NguoiDungController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, InstantiationException, SQLException, IllegalAccessException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        try {
            /* TODO output your page here. You may use following sample code. */
            if(session.getAttribute("USERNAME")!=null){
            String action = request.getParameter("action");
            
             
           /*   int pageSize = 10;
            int pageNumber = 1;
            
            int soTrang;
           // int soluongnguoidung = NguoiDungDAO.SoLuongNguoiDung();
         if(soluongnguoidung % pageSize == 0){
                soTrang = soluongnguoidung/pageSize;
            }else{
                soTrang = soluongnguoidung/pageSize + 1;
            }
            request.setAttribute("sotrang", soTrang);
            
            if(request.getParameter("page") != null){
                pageNumber = Integer.parseInt(request.getParameter("page"));
            }
            */
            //lay danh sach loai nguoi dung do len combobox
            List<LoaiNguoiDung> dslnd = LoaiNguoiDungDAO.LayDSLoaiNguoiDung();
            request.setAttribute("dslnd", dslnd);
            
            if(action == null){
                List<NguoiDung> dsnd = NguoiDungDAO.LayDanhSachNguoiDung();
                request.setAttribute("dsnd", dsnd);
                request.getRequestDispatcher("nguoidung.jsp").forward(request, response);
            } else if(action.equals("insert")){
                request.setAttribute("view", false);
                session.setAttribute("save", 0);
                request.getRequestDispatcher("thongtinnguoidung.jsp").forward(request, response);
            } else if(action.equals("Save")){
                
                NguoiDung nd;
                if((Integer)session.getAttribute("save") == 0){
                    nd = new NguoiDung();
                    nd.setTendangnhap(request.getParameter("txttendangnhap"));
                }else{
                    nd = NguoiDungDAO.LayThongTinNguoiDung(request.getParameter("txttendangnhap"));
                }
                nd.setMatkhau(request.getParameter("txtmatkhau"));
                nd.setHoten(request.getParameter("txthoten"));
                nd.setEmail(request.getParameter("txtemail"));
                nd.setDienThoai(request.getParameter("txtdienthoai"));
                int gioitinh = Integer.parseInt(request.getParameter("rdgioitinh"));
                nd.setGioiTinh(gioitinh);
                nd.setNgaysinh(request.getParameter("txtngaysinh"));
               // String ngaysinhstr = request.getParameter("txtngaysinh");
                //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
               /* try {
                   Date d = sdf.parse(ngaysinhstr);
                    nd.setNgaysinh(d);
                } catch (ParseException ex) {
                    //nd.setNgaysinh(null);
                    Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
                } */
                int trangthai = Integer.parseInt(request.getParameter("rdtrangthai"));
                nd.setTrangthai(trangthai);
                int maloainguoidung = Integer.parseInt(request.getParameter("cmdloainguoidung"));
                nd.setMaloainguoidung(maloainguoidung);
               // LoaiNguoiDung lnd = LoaiNguoiDungDAO.LayThongTinLoaiNguoiDung(maloainguoidung);
                //nd.setLoaiNguoiDung(lnd);
                if((Integer)session.getAttribute("save") == 0){
                    NguoiDungDAO.ThemNguoiDung(nd);
                } else {
                    NguoiDungDAO.CapNhatNguoiDung(nd);
                }
                List<NguoiDung> dsnd = NguoiDungDAO.LayDanhSachNguoiDung();
                request.setAttribute("dsnd", dsnd);
                request.getRequestDispatcher("nguoidung.jsp").forward(request, response);
            } else if(action.equals("view")){
                request.setAttribute("view", true);
                NguoiDung nd = NguoiDungDAO.LayThongTinNguoiDung(request.getParameter("id"));
                 request.setAttribute("nguoidungedit", nd);
                
                request.getRequestDispatcher("thongtinnguoidung.jsp").forward(request, response);
                
            } else if(action.equals("edit")){
                request.setAttribute("view", false);
                session.setAttribute("save", 1);
                NguoiDung nd = NguoiDungDAO.LayThongTinNguoiDung(request.getParameter("id"));
                request.setAttribute("nguoidungedit", nd);
                request.getRequestDispatcher("thongtinnguoidung.jsp").forward(request, response);
            } else if(action.equals("Delete")){
                String[] checks = request.getParameterValues("delete");
                
                if(checks!=null){
                    for(int i=0; i < checks.length; i++){
                        NguoiDungDAO.XoaNguoiDung(checks[i]);
                    }
                }
                
                List<NguoiDung> dsnd = NguoiDungDAO.LayDanhSachNguoiDung();
                request.setAttribute("dsnd", dsnd);
                request.getRequestDispatcher("nguoidung.jsp").forward(request, response);
              
            }
            } else {
                response.sendRedirect("Login");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(NguoiDungController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
